package com.acwer.framework.data;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;


import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.annotation.TableId;


public class DataColumnUtils {
	/**
	 * 获取所有字段，包括私有、公有
	 * @param modelType
	 * @return
	 */
	public static List<Field> getAllFields(Class modelType){
		
		List<Field>fields=new ArrayList<Field>();
		List<Class> clzs=new ArrayList<Class>();
		Class tmpClz=modelType;
		do {
			clzs.add(tmpClz);
			tmpClz=tmpClz.getSuperclass();
		} while (tmpClz!=null&&tmpClz!=Object.class);
		Collections.reverse(clzs);
		
		for (Class clz : clzs) {
			fields.addAll(Arrays.asList(clz.getDeclaredFields()));
		}
		return fields;
		
	}
	/**
	 * 
	 * @param clz
	 * @return
	 */
	public static DataColumn primayKey(Class<?> clz) {
		Optional<DataColumn> oneOptional= DataColumnUtils.getColumnsByClzz(clz).stream().filter(it->it.isPrimaryKey()).findFirst();
		if(!oneOptional.isPresent()) return null;
		return oneOptional.get();
	}
	private static ConcurrentHashMap<String, List<DataColumn>> hashMap= new ConcurrentHashMap<String, List<DataColumn>>();
	/**
	 * 通过model类型获取字段描述信息
	 * @param modelType
	 * @return
	 */
	public static List<DataColumn> getColumnsByClzz(Class modelType) {
		
		List<DataColumn> columns=new ArrayList<DataColumn>();
		
		String cacheColKey=modelType.getName();
		
		if(hashMap.containsKey(cacheColKey)){
			columns=hashMap.get(cacheColKey);
		}else {
			//Class tmpClz=modelType;
			List<Field>fields=getAllFields(modelType);
			
			
			
			for (Field field : fields) {
				
				DataColumn column=new DataColumn();
				//字段名称
				column.setColumnName(field.getName());
				//字段类型
				column.setDataType(field.getType());
				column.setStrDataType(field.getType().getSimpleName().toLowerCase());
				//字段显示名称
				String display=column.getColumnName();
				
				ColumnExplain explain=field.getAnnotation(ColumnExplain.class);
				if(explain!=null) display=explain.value();
				column.setDisplay(display);
				//是否在列表里面显示
				if(explain!=null) column.setLs(explain.ls());
				
				//字段显示顺序
				int order=fields.indexOf(field)+1;
				if(explain!=null&&explain.order()!=0) column.setOrder(explain.order());
				else column.setOrder(order);
				
				//控件类型，用某个控件显示字段值
				if(explain!=null){
					column.setControlType(explain.dataType());
					column.setSearch(explain.search());
					column.setDisable(explain.disable());
					//column.setBindValue(explain.bindVal());
				}
				 
				//是否只读
				if(explain!=null) column.setReadOnly(!explain.isEdit());
				
				if(explain!=null) column.setDataTokens(explain.dataTokens());
				//字段显示样式
				//设置长度、对齐方式
				if(explain!=null){
					column.setWidth(explain.width());
					column.setAlign(explain.align());
				}
				if(StringUtils.isEmpty(column.getControlType())){
					if(field.getType()==boolean.class||field.getType()==Boolean.class){
						column.setControlType("switch");
					}
					if(field.getType()==Date.class){
						column.setControlType("date");
					}
					if(field.getType()==Integer.class
							||field.getType()==Double.class
							||field.getType()==Long.class
							){
						column.setControlType("number");
					}
				}
				
				TableId id=field.getAnnotation(TableId.class);
				if(id!=null){
					column.setPrimaryKey(true);
					column.setRequired(true);
				}else if (explain!=null) {
					column.setRequired(explain.required());
				}
				
				
				columns.add(column);
			}
			columns.sort((a,b)->a.getOrder()-b.getOrder());
			
			hashMap.put(cacheColKey, columns);
		}
		return columns;
		
//		//Class tmpClz=modelType;
//		List<Field>fields=getAllFields(modelType);
//		
//		
//		
//		for (Field field : fields) {
//			
//			DataColumn column=new DataColumn();
//			//字段名称
//			column.setColumnName(field.getName());
//			//字段类型
//			column.setDataType(field.getType());
//			column.setStrDataType(field.getType().getSimpleName().toLowerCase());
//			//字段显示名称
//			String display=column.getColumnName();
//			
//			ColumnExplain explain=field.getAnnotation(ColumnExplain.class);
//			if(explain!=null) display=explain.value();
//			column.setDisplay(display);
//			//是否在列表里面显示
//			if(explain!=null) column.setLs(explain.ls());
//			
//			//字段显示顺序
//			int order=fields.indexOf(field)+1;
//			if(explain!=null&&explain.order()!=0) column.setOrder(explain.order());
//			else column.setOrder(order);
//			
//			//控件类型，用某个控件显示字段值
//			if(explain!=null){
//				column.setControlType(explain.dataType());
//				column.setSearch(explain.search());
//			}
//			 
//			//是否只读
//			if(explain!=null) column.setReadOnly(!explain.isEdit());
//			
//			if(explain!=null) column.setDataTokens(explain.dataTokens());
//			//字段显示样式
//			//设置长度、对齐方式
//			if(explain!=null){
//				column.setWidth(explain.width());
//				column.setAlign(explain.align());
//			}
//			if(StringUtil.isEmpty(column.getControlType())){
//				if(field.getType()==boolean.class||field.getType()==Boolean.class){
//					column.setControlType("switch");
//				}
//				if(field.getType()==Date.class){
//					column.setControlType("date");
//				}
//				if(field.getType()==Integer.class
//						||field.getType()==Double.class
//						||field.getType()==Long.class
//						){
//					column.setControlType("number");
//				}
//			}
//			
//			Id id=field.getAnnotation(Id.class);
//			if(id!=null){
//				column.setPrimaryKey(true);
//				column.setRequired(true);
//			}else if (explain!=null) {
//				column.setRequired(explain.required());
//			}
//			
//			
//			columns.add(column);
//		}
//		columns.sort((a,b)->a.getOrder()-b.getOrder());
//		return columns;
	}
}
